﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dukou.DTO;
using Dukou.Example.DTO.Domain;
using Dukou.Example.DTO.DTO;

namespace Dukou.Example.DTO.Parsers
{
    public class UserParser : ObjectParser<UserDTO, User>
    {
        protected override UserDTO InternalParse(User k, IDictionary<string, ObjectCache> temp, int depth)
        {
            UserDTO dto = new UserDTO()
            {
                CreateTime = k.CreateTime,
                UpdateTime = k.UpdateTime,
                Id = k.Id,
                LocalId = Guid.NewGuid(),
                Password = k.Password,
                UserName = k.UserName
            };

            temp.Add(k.GetIdentity(), new ObjectCache(depth, dto));

            dto.Creator = ObjectParser.Parse<UserDTO, User, UserParser>(k.Creator, temp, depth - 1);
            dto.Updator = ObjectParser.Parse<UserDTO, User, UserParser>(k.Updator, temp, depth - 1);

            return dto;
        }

        protected override User InternalParse(UserDTO t, IDictionary<string, ObjectCache> temp, int depth)
        {
            User user = new User() 
            {
                CreateTime = t.CreateTime,
                UpdateTime = t.UpdateTime,
                Id = t.Id,
                Password = t.Password,
                UserName = t.UserName
            };

            temp.Add(t.GetIdentity(), new ObjectCache( depth, user));

            user.Creator = ObjectParser.Parse<UserDTO, User, UserParser>(t.Creator, temp, depth - 1);
            user.Updator = ObjectParser.Parse<UserDTO, User, UserParser>(t.Updator, temp, depth - 1);

            return user;
        }
    }
}
